package com.xd.study.activity;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.xd.study.R;

/**
 * Created by DGF on 2015/5/5.
 */
public class ContentProviderActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_provider);

        Button btnQuery1 = (Button)this.findViewById(R.id.content_provider_btn_query1);
        Button btnQuery2 = (Button)this.findViewById(R.id.content_provider_btn_query2);

        /**
         * 获取全部的联系人
         */
        btnQuery1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ContentResolver contentResolver = getContentResolver();
                //查询联系人的id和名称
                Cursor cursor = contentResolver.query(
                        ContactsContract.Contacts.CONTENT_URI,
                        new String[]{ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME},
                        null,
                        null,
                        null);
                if (cursor != null){
                    while(cursor.moveToNext()){
                        int id = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts._ID));
                        String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

                        Log.i("ContentProvider", "id = "+id+"; name = "+name);

                        //查询电话号码及类型
                        Cursor cursor1 = contentResolver.query(
                                ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.TYPE},
                                ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"="+id,
                                null,
                                null);

                        if (cursor1 != null){
                            while(cursor1.moveToNext()){
                                String number = cursor1.getString(cursor1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                                int type = cursor1.getInt(cursor1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));

                                if (type == ContactsContract.CommonDataKinds.Phone.TYPE_HOME){
                                    Log.i("ContentProvider", "家庭电话："+number);
                                }else if (type == ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE){
                                    Log.i("ContentProvider", "移动电话："+number);
                                }else{
                                    Log.i("ContentProvider", "其他电话："+number);
                                }
                            }
                            cursor1.close();
                        }

                        //查询邮箱
                        Cursor cursor2 = contentResolver.query(
                                ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                                new String[]{ContactsContract.CommonDataKinds.Email.ADDRESS, ContactsContract.CommonDataKinds.Email.TYPE},
                                ContactsContract.CommonDataKinds.Email.CONTACT_ID+"="+id,
                                null,
                                null);
                        if (cursor2 != null){
                            while(cursor2.moveToNext()){
                                String address = cursor2.getString(cursor2.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS));
                                int type = cursor2.getInt(cursor2.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));

                                if (type == ContactsContract.CommonDataKinds.Email.TYPE_WORK){
                                    Log.i("ContentProvider", "工作邮箱："+address);
                                }else if (type == ContactsContract.CommonDataKinds.Email.TYPE_HOME){
                                    Log.i("ContentProvider", "家庭邮箱："+address);
                                }else{
                                    Log.i("ContentProvider", "其他邮箱："+address);
                                }
                            }
                            cursor2.close();
                        }
                    }
                    Toast.makeText(ContentProviderActivity.this, "查询联系人成功，联系人个数为："+cursor.getCount(), Toast.LENGTH_SHORT).show();
                    cursor.close();
                }else{
                    Toast.makeText(ContentProviderActivity.this, "无联系人信息", Toast.LENGTH_SHORT).show();
                }
            }
        });

        /**
         * 插入联系人
         */
        btnQuery2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                ContentResolver contentResolver = getContentResolver();

                ContentValues contentValues = new ContentValues();

                Uri uri = contentResolver.insert(ContactsContract.RawContacts.CONTENT_URI, contentValues);
                long raw_contact_id = ContentUris.parseId(uri);

                //插入人名
                contentValues.clear();
                contentValues.put(ContactsContract.CommonDataKinds.StructuredName.RAW_CONTACT_ID, raw_contact_id);
                contentValues.put(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, "jamos");
                contentValues.put(ContactsContract.CommonDataKinds.StructuredName.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
                uri = contentResolver.insert(ContactsContract.Data.CONTENT_URI, contentValues);

                //插入电话
                contentValues.clear();
                contentValues.put(ContactsContract.CommonDataKinds.Phone.RAW_CONTACT_ID, raw_contact_id);
                contentValues.put(ContactsContract.CommonDataKinds.Phone.NUMBER, "15989822323");
                contentValues.put(ContactsContract.CommonDataKinds.Phone.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
                contentValues.put(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE);
                uri = contentResolver.insert(ContactsContract.Data.CONTENT_URI, contentValues);

                Toast.makeText(ContentProviderActivity.this, "插入联系人成功", Toast.LENGTH_SHORT).show();
            }
        });
    }
}
